<?php
namespace api\modules\sms\impcommon;
use api\modules\sms\icommon\Isms;
use api\modules\sms\models\WhoSmsLog;
use api\helpers\SmsHelper;
/**
 * User: ziwen
 * Date: 2016/1/28 11:42
 * File: splio.php
 * Desc: ""
 */
class splio implements Isms{
    private static $instance = null;

    private function __construct(){
    }

    public static function getInstance()
    {
        if(self::$instance){
            return self::$instance;
        }else{
            self::$instance = new self();
            return self::$instance;
        }
    }

    private static $apiUrl = 'https://api.spl4cn.com/api/triggersms/nph-1.pl?';

    private static $universe = 'jollychicforward_cn';
    private static $key = 'ba11983eed59569e5cfad4bce31ce224d8002304';
    private $message;
    private $contact;

    private $firstname;
    private $lastname;
    private $email;
    private $cxx;
    private $requestUrl;

    //设置数据
    private function setData($data){
        $this->setMessage($data);
        $this->setContact($data);
        $this->setFirstname($data);
        $this->setLastname($data);
        $this->setCxx($data);
        $this->setEmail($data);

        //构造要发送的数据
        $this->buildUrl();
    }

    /**
     * @desc 构造发送短信的参数
     */
    private function buildUrl(){
        $dataList = array();
        $dataList['universe'] = self::getUniverse();
        $dataList['key'] = self::getKey();
        $dataList['message'] = $this->getMessage();
        $dataList['contact'] = $this->getContact();

        $dataList['firstname'] = $this->getFirstname();
        $dataList['lastname'] = $this->getLastname();
        $dataList['email'] = $this->getEmail();
        $dataList['cxx'] = $this->getCxx();

        //过滤掉没有参数的项
        $dataList = array_filter($dataList);
        $queryCxxStr = '';
        if($dataList['cxx']){
            $queryCxxStr = '&'. $dataList['cxx'];
            unset($dataList['cxx']);
        }
        $queryUrl = http_build_query($dataList);
        $this->requestUrl = self::getApiUrl().$queryUrl.$queryCxxStr;
        return $this->requestUrl;
    }

    private static function getKey()
    {
        return self::$key;
    }
    /**
     * @return string
     */
    private static function getApiUrl()
    {
        return self::$apiUrl;
    }

    /**
     * @return mixed
     */
    private function getUniverse()
    {
        return self::$universe;
    }

    /**
     * @return mixed
     */
    private function getMessage()
    {
        return $this->message;
    }

    /**
     * @param mixed $message
     */
    private function setMessage($data)
    {
        $message = isset($data['message']) && $data['message'] ? trim($data['message']) : '';
        if(!$message) return false;
        $this->message = $data['message'];
    }

    /**
     * @return mixed
     * @desc 获取电话
     */
    private function getContact()
    {
        return $this->contact;
    }

    /**
     * @param mixed $contact
     */
    private function setContact($data)
    {
        $contact = isset($data['contact']) && $data['contact'] ? trim($data['contact']) : '';
        if(!$contact) return false;
        $this->contact = $contact;
    }

    /**
     * @return mixed
     */
    private function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * @param mixed $firstname
     */
    private function setFirstname($data)
    {
        $firstname = isset($data['firstname']) && $data['firstname'] ? trim($data['firstname']) : '';
        $this->firstname = $firstname;
    }

    /**
     * @return mixed
     */
    private function getLastname()
    {
        return $this->lastname;
    }

    /**
     * @param mixed $lastname
     */
    private function setLastname($data)
    {
        $lastname = isset($data['lastname']) && $data['lastname'] ? trim($data['lastname']) : '';
        $this->lastname = $lastname;
    }

    /**
     * @return array
     */
    private function getCxx()
    {
        if($this->cxx && is_array($this->cxx) && count($this->cxx)>0){
            $this->cxx = http_build_query($this->cxx);
        }
        return $this->cxx;
    }

    /**
     * @param $data
     */
    private function setCxx(array $data)
    {
        $cxxList = array();
        $cxx = isset($data['cxx']) && $data['cxx'] ? $data['cxx'] : array();
        if(is_array($cxx) && count($cxx)){
            foreach($cxx as $k=>$v){
                $cxxList[$k] = $v;
            }
        }
        $this->cxx = $cxxList;
    }

    /**
     * @return mixed
     */
    private function getEmail()
    {
        return $this->email;
    }

    /**
     * @param mixed $email
     */
    private function setEmail($data)
    {
        $email = isset($data['email']) && $data['email'] ? trim($data['email']) : '';
        $this->email = $email;
    }

    /**
     * @desc 发送信息
     */
    public function sendMsg(array $data){
        $this->setData($data);
        $status = 0;
        $url = $this->requestUrl;

        $ret = self::sendHttpsUrl($url);
        if($ret['httpcode'] == 200){
            $response = json_decode($ret['return_message'],true);
            if($response['status'] == 'ok' && $response['code'] == 200 ){
                $status = 1;
            }
        }

        $parmars = array(
            'universe'=>$this->getUniverse(),
            'key'=>$this->getKey(),
            'message'=>$this->getMessage(),
            'contact'=>$this->getContact(),
            'firstname'=>$this->getFirstname(),
            'lastname'=>$this->getLastname(),
            'email'=>$this->getEmail(),
            'cxx'=>$this->getCxx(),
        );

        $data2 = array(
            'mobile'=>$this->getContact(),
            'query_url'=>$this->getRequestUrl(),
            'log_time'=>time(),
            'status'=>$status,
            'return_message'=>$ret['return_message'],
            'type'=>0,
            'channel'=>1,
            'source_id'=>$data['source_id'],
            'content'=>$data['verify_code'],
        );

        $smsLogModel = new WhoSmsLog();
        $smsLogModel->setAttributes($data2);
        if(!$smsLogModel->save()){
            SmsHelper::pr($smsLogModel->getErrors());
        }
        exit;
        //return array('status'=>$status,'smsid'=>$smsId);
    }


    private function getRequestUrl(){
        return $this->requestUrl;
    }

    private static function sendHttpsUrl($url){
        $timeOut = 60;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        //不进行ssl的认证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        curl_close($ch);

        $returnArr = array('httpcode'=>$httpCode,'return_message'=>$ret);
        return $returnArr;
    }

}


